package com.spzx.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Arrays;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.common.core.context.SecurityContextHolder;
import com.spzx.common.core.utils.DateUtils;
import com.spzx.user.service.IRegionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.spzx.user.mapper.UserAddressMapper;
import com.spzx.user.domain.UserAddress;
import com.spzx.user.service.IUserAddressService;

/**
 * 用户地址Service业务层处理
 *
 * @author atguigu
 * @date 2024-09-27
 */
@Service
public class UserAddressServiceImpl extends ServiceImpl<UserAddressMapper, UserAddress> implements IUserAddressService {

    @Autowired
    private UserAddressMapper userAddressMapper;

    @Autowired
    private IRegionService regionService;

    /**
     * 查询用户地址列表
     *
     * @return 用户地址
     */
    @Override
    public List<UserAddress> selectUserAddressList() {
        Long userId = SecurityContextHolder.getUserId();
        LambdaQueryWrapper<UserAddress> userAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userAddressLambdaQueryWrapper.eq(UserAddress::getUserId, userId);
        return userAddressMapper.selectList(userAddressLambdaQueryWrapper);
    }

    /**
     * 新增用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int insertUserAddress(UserAddress userAddress) {
        Long userId = SecurityContextHolder.getUserId();
        userAddress.setUserId(userId);
        String province = regionService.getNameByCode(userAddress.getProvinceCode());
        String city = regionService.getNameByCode(userAddress.getCityCode());
        String district = regionService.getNameByCode(userAddress.getDistrictCode());
        String address = userAddress.getAddress();
        String fullAddress = province + city + district + address;
        userAddress.setFullAddress(fullAddress);
        userAddress.setCreateTime(new Date());
        if (userAddress.getIsDefault().intValue() == 1) {
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0);
            LambdaQueryWrapper<UserAddress> userAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userAddressLambdaQueryWrapper.eq(UserAddress::getUserId, userAddress.getUserId());
            userAddressMapper.update(updateUserAddress, userAddressLambdaQueryWrapper);
        }
        return userAddressMapper.insert(userAddress);
    }


    /**
     * 修改用户地址
     *
     * @param userAddress 用户地址
     * @return 结果
     */
    @Override
    public int updateUserAddress(UserAddress userAddress) {
        String province = regionService.getNameByCode(userAddress.getProvinceCode());
        String city = regionService.getNameByCode(userAddress.getCityCode());
        String district = regionService.getNameByCode(userAddress.getDistrictCode());
        String address = userAddress.getAddress();
        String fullAddress = province + city + district + address;
        userAddress.setFullAddress(fullAddress);
        if (userAddress.getIsDefault().intValue() == 1){
            UserAddress updateUserAddress = new UserAddress();
            updateUserAddress.setIsDefault(0);
            LambdaQueryWrapper<UserAddress> userAddressLambdaQueryWrapper = new LambdaQueryWrapper<>();
            userAddressLambdaQueryWrapper.eq(UserAddress::getUserId, userAddress.getUserId());
            userAddressMapper.update(updateUserAddress, userAddressLambdaQueryWrapper);
        }
        return userAddressMapper.updateById(userAddress);
    }

}
